package com.tianlan.blog.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.tianlan.blog.entity.SysDict;
import com.tianlan.blog.service.SysDictService;
import com.tianlan.common.annotation.log.AroundLog;
import com.tianlan.common.base.Result;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 数据字典表 前端控制器
 * </p>
 *
 * @author tl-wxq
 * @since 2018-10-28
 */
@RestController
@RequestMapping("/common/dict")
public class DictController {

    @Autowired
    private SysDictService sysDictService;

    /**
     * 查询字典里列表
     * 
     * @param parentId
     * @return
     */
    @GetMapping(value = "/tree")
    @RequiresRoles("SYSADMIN")
    @ApiOperation(value = "查询字典树列表", notes = "查询字典树列表")
    @ApiImplicitParam(paramType = "query", name = "parentId", value = "父级ID", required = false, dataType = "Integer")
    public Result<List<SysDict>> queryDictTree(Integer parentId) {
        List<SysDict> treeList = sysDictService.queryDictTreeByPanentId(parentId);
        return new Result<List<SysDict>>().success().put(treeList);
    }

    /**
     * 添加字典
     */
    @PostMapping("/create")
    @RequiresRoles("SYSADMIN")
    @ApiOperation(value = "添加字典")
    @AroundLog(name = "添加字典")
    public Result<SysDict> create(@RequestBody SysDict org) {
        boolean result = sysDictService.saveDict(org);
        if (result) {
            return new Result<SysDict>().success("添加成功").put(org);
        } else {
            return new Result<SysDict>().error("添加失败，请重试");
        }
    }

    /**
     * 修改字典
     */
    @PostMapping("/update")
    @RequiresRoles("SYSADMIN")
    @ApiOperation(value = "更新字典")
    @AroundLog(name = "更新字典")
    public Result<SysDict> update(@RequestBody SysDict org) {
        boolean result = sysDictService.updateDict(org);
        if (result) {
            return new Result<SysDict>().success("修改成功").put(org);
        } else {
            return new Result<SysDict>().error("修改失败");
        }
    }

    /**
     * 删除字典
     */
    @PostMapping("/delete/{orgId}")
    @RequiresRoles("SYSADMIN")
    @ApiOperation(value = "删除字典")
    @AroundLog(name = "删除字典")
    @ApiImplicitParam(paramType = "path", name = "orgId", value = "字典ID", required = true, dataType = "Integer")
    public Result<?> delete(@PathVariable("orgId") Integer orgId) {
        boolean result = sysDictService.deleteDict(orgId);
        if (result) {
            return new Result<>().success("删除成功");
        } else {
            return new Result<>().error("删除失败");
        }
    }
//    
//    /**
//     * 查询字典里列表
//     *
//     * @param dictCode
//     * @return
//     */
//    @PostMapping(value = "/list/{dictCode}")
//    @NoAuthentication
//    @ApiOperation(value = "查询字典列表", notes = "查询字典列表")
//    @ApiImplicitParam(paramType = "query", name = "dictCode", value = "字典值", required = true, dataType = "String")
//    public Result<List<Dict>> queryDictList(@PathVariable("dictCode") String dictCode) {
//        List<Dict> dictList = dictService.queryDictListByPanentCode(dictCode);
//        return new Result<List<Dict>>().success().put(dictList);
//    }
    
}
